import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

import persist from "vuex-persist";

export default new Vuex.Store({
    state: {
        shoplist: [],
        user:""
    },
    mutations: {
        add(state, item) {
            // state.shoplist.push(item)
            console.log(item);
            let flag = false;
            state.shoplist.forEach((i) => {
                if (i.id == item.id) {
                    flag = true;
                    item.num++;
                }
            });
            if (flag == false) {
                item.num = 1;
                state.shoplist.push(item);
            }
        },
        handleChange(state,obj){
          // console.log(obj)
          state.shoplist.forEach(item=>{
            if(item.id==obj.id){
              item.num=obj.num
            }
          })
        },
        token(state,obj){
          console.log(obj)
          state.user=obj
        },
        tui(state){
          state.user=""
         
        }
    },
    actions: {},
    modules: {},

    plugins: [
        new persist({
            storage: window.localStorage,
        }).plugin,
    ],
});
